package com.example.dairenkun.neufoodshop.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.activity.BuyActivity;
import com.example.dairenkun.neufoodshop.activity.FoodActivity;
import com.example.dairenkun.neufoodshop.activity.FoodDetailActivity;
import com.example.dairenkun.neufoodshop.activity.LoginActivity;
import com.example.dairenkun.neufoodshop.activity.MainActivity;
import com.example.dairenkun.neufoodshop.activity.RegisterActivity;
import com.example.dairenkun.neufoodshop.bean.Buy;
import com.example.dairenkun.neufoodshop.bean.Food;
import com.example.dairenkun.neufoodshop.bean.Order;
import com.example.dairenkun.neufoodshop.model.OrderModel;
import com.example.dairenkun.neufoodshop.service.FoodService;
import com.example.dairenkun.neufoodshop.service.OrderService;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

;import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/3/6.
 */

public class FoodListAdapter extends RecyclerView.Adapter<ViewHolder> {
    int userid;
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public FoodListAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.inflate(R.layout.cardview_food, parent, false);
        return new ViewHolder(v);


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       final   Food entity=(Food) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.food_name.setText(entity.getFoodname());
       viewHolder.food_price.setText(entity.getPrice()+"元");
        if (!"".equals(entity.getPic()))
        Picasso.with(mContext).load(entity.getPic()).into(viewHolder.food_image);
        //购买
        viewHolder.btn_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,BuyActivity.class);
                intent.putExtra("food_id",entity.getFood_id()+"");
                intent.putExtra("price",entity.getPrice()+"");
                intent.putExtra("foodname",entity.getFoodname());
                intent.putExtra("pic",entity.getPic());
                mContext.startActivity(intent);
                  /*  getUser();
                     final int foodid=entity.getFood_id();
                int money=entity.getPrice();
                double sum=(double) money;
                SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                final String suggesttime = formatter.format(curDate);
                //获取网上的数据/
                OrderModel orderModel=new OrderModel();
                Call call=orderModel.Buy(userid,foodid,1,sum,suggesttime);
                call.enqueue(new Callback<Buy>() {
                    @Override
                    public void onResponse(Call<Buy> call, retrofit2.Response<Buy> response)
                    {
                        Buy buy=response.body();
                       String result=buy.getSuccess();
                        if("0".equals(result))
                        {
                            //失败处理
                            Toast.makeText(mContext, "支付失败！", Toast.LENGTH_SHORT).show();
                        } else
                        {
                            //登录成功处理
                            Toast.makeText(mContext, "购买成功！"+userid+"$"+foodid+suggesttime, Toast.LENGTH_SHORT).show();
                               //用户Id 的值不对，  sum  值不对
                        }


                    }
                    @Override
                    public void onFailure(Call<Buy> call, Throwable t) {

                    }
                });*/

            }
        });
        //点击进入详情
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,FoodDetailActivity.class);
                intent.putExtra("food_id",entity.getFood_id()+"");
                mContext.startActivity(intent);

            }
        });
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView food_name;
        ImageView food_image;
        TextView food_price;
        Button btn_buy;



        public ViewHolder(View itemView) {
            super(itemView);
            food_name = (TextView) itemView.findViewById(R.id.food_name);
            food_image = (ImageView) itemView.findViewById(R.id.food_image);
            food_price = (TextView) itemView.findViewById(R.id.food_money);
            btn_buy=(Button)itemView.findViewById(R.id.btn_food_buy);
           /* itemView.findViewById(R.id.food_content).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //
                }
            });*/

        }

    }
    private void getUser(){
        //context=getActivity();
        SharedPreferences sp = mContext.getSharedPreferences("UserInfo", mContext.MODE_PRIVATE);
        userid = Integer.valueOf(sp.getString("user_id", null));
        //Toast.makeText(context,userid,Toast.LENGTH_SHORT).show();

    }
}
